
/////////////////////////////////////////////////////////////////////
// GET REQUESTS

function getHTTPObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return null;
}

function get_request(method,uri,data,callback) {
 	req = getHTTPObject();
	req.open(method,uri,true);
	if(callback) {
		req.onreadystatechange = function(r) {
			if(req.readyState == 4) {
				callback(req.status,req.responseText);
			}
		}
	}
	req.send(data);
	return req;
}

/////////////////////////////////////////////////////////////////////
// CALLBACKS

function callback_reset_refresh(status,data) {
	// check status code
	if(status != 200) {
		console.log("callback_reset_refresh: HTTP status code " + status);
		return;
	}
	// Need to reload the IFRAME
	iframeNode = document.getElementById("template");
	if(iframeNode) {
		if(navigator.appName == "Microsoft Internet Explorer") {
		    iframeNode.contentWindow.location.reload(true);
		} else {
		    iframeNode.src = iframeNode.src;
		}
	} else {
		console.log("api_reset_refresh: unable to refresh IFRAME")
	}
}

function callback_get_state_done(status,data) {
	// check status code
	if(status != 200) {
		console.log("callback_get_state_done: HTTP status code " + status);
		return;
	}
	var json = eval('('+ data +')');
	for(key in json) {
		value = json[key];
		console.log("callback_get_state_done => " + key + " => " + value);
	}
}

/////////////////////////////////////////////////////////////////////
// API CALLS

function api_reset() { get_request("PUT","/reset",null,callback_reset_refresh); }
function api_get_state() { get_request("GET","/state.js",null,callback_get_state_done); }


